package com.wyf.video
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.wyf.video.adapter.NewAdapter
import com.wyf.video.global.Const
import com.wyf.video.model.Bt
import com.wyf.video.model.Result
import com.zhy.http.okhttp.OkHttpUtils
import kotlinx.android.synthetic.main.fragment_new.*
import org.jetbrains.anko.sdk25.coroutines.onItemClick
import org.jetbrains.anko.support.v4.onRefresh
import org.jetbrains.anko.support.v4.toast
import com.zhy.http.okhttp.callback.StringCallback
import okhttp3.Call

class NewFragment: Fragment(){

    var mContext:Context?=null
    var btList =  ArrayList<Bt>()
    lateinit var adapter: NewAdapter
    var page =0

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_new, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initSwipe()
        initGridView()

        loadData(page++)
    }

    @SuppressLint("ResourceAsColor")
    private fun initSwipe() {
        new_swipe.setColorSchemeColors(R.color.colorAccent)
        new_swipe.onRefresh {
            page =0
            btList.clear()
            loadData(page++)
            new_swipe.isRefreshing = false
        }
    }

    private fun loadData(p: Int) {
        val url = "http://47.100.23.73:1324/new/"+p+"/10"
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(object : StringCallback() {
                    override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                        Log.i("wyf",""+e.toString())
                    }

                    override fun onResponse(response: String?, id: Int) {
                        Log.i("wyf",""+response)
                        var g = Gson()
                        var result = g.fromJson<Result>(response,object : TypeToken<Result>() {}.type)
                        if(result.code==0){

                            btList.addAll(result.data)
                            if(adapter==null){
                                adapter = NewAdapter(mContext,btList)
                            }
                            adapter.notifyDataSetChanged()
                        }else{
                            toast(result.msg)
                        }

                    }
                })
    }


    private fun initGridView() {

        adapter = NewAdapter(mContext,btList)
        new_gridview.adapter = adapter

        new_gridview.onItemClick { p0, p1, p2, p3 ->
            val intent = Intent(mContext,DetailActivity::class.java)
            startActivity(intent)
        }
        new_gridview.setOnScrollListener(object : AbsListView.OnScrollListener {
            override fun onScroll(p0: AbsListView?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onScrollStateChanged(p0: AbsListView?, p1: Int) {
                if(p1 == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
                    if (new_gridview?.getLastVisiblePosition() == (new_gridview.getCount() - 1)) {
                            loadData(page++)
                            //adapter.notifyDataSetChanged()
                    }
                }
            }

        })

        new_gridview.setOnItemClickListener { parent, view, position, id ->
            val intent = Intent()
            intent.setClass(context, DetailActivity::class.java!!)

            intent.putExtra(Const.PASS_BT,Gson().toJson(btList.get(position)))
            startActivity(intent)
        }

    }
}

